from selenium import webdriver
import time
import selenium
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as Wait


def switch_page(driver, click_ele=None, click_ele_id=None):
    if click_ele:
        if click_ele_id is not None:
            Wait(driver, 60).until(
                EC.presence_of_element_located((By.ID, click_ele_id)))
        driver.implicitly_wait(10)
        time.sleep(2)
        click_ele.click()
    time.sleep(5)
    driver.switch_to.window(driver.window_handles[-1])


def login_part(driver: webdriver.chrome.webdriver.WebDriver):
    # driver = webdriver.Chrome()
    logged_in = False
    page_num = 0
    time.sleep(1)
    login_icon = driver.find_element_by_class_name('login-icon')
    login_icon.click()
    while not logged_in:
        for i, page in enumerate(driver.window_handles):
            driver.switch_to.window(page)
            try:
                text = driver.find_element_by_class_name('logged-text')
                logged_in = True
                page_num = i
                print(f'catched log in on {page}')
                break
            except:
                logged_in = False
        print('waiting for logging in...')
        time.sleep(1)
    time.sleep(1)
    return page_num


def read_articles(driver):
    # driver = webdriver.Chrome()
    print('reading articles start')
    # articles_set_xpath = '//*[@id="V7vRVMl_aq"]/div/div/div/div/div/section/div/div/div/div'
    # for article_id in range(1, 7):
    #     child_xpath = articles_set_xpath + f'/div[{article_id}]'
    #     article = driver.find_element_by_xpath(child_xpath)
    #     text_ele = driver.find_element_by_xpath(child_xpath + '/div/div/div/span')
    #     print(f'start read article {text_ele.text}...')
    #     switch_page(driver, article)
    #     time.sleep(5)
    #     # 滚动
    #     driver.execute_script('document.documentElement.scrollTop=10000')
    #     time.sleep(60)
    #     driver.close()
    #     print('read done')
    #     switch_page(driver)
    articles_set_xpath = '//*[@id="root"]/div/div/section/div/div/div/div/div/section/div/div/div/div[1]/div/section/div/div/div/div/div/section/div/div/div/div/div[3]/section/div/div/div/div/div/section/div/div/div[1]/div'
    for article_id in range(1, 7):
        child_xpath = articles_set_xpath + f'/div[{article_id}]'
        article = driver.find_element_by_xpath(child_xpath)
        text_ele = article.find_element_by_class_name('text-wrap')
        print(f'start read article {text_ele.text}...')
        switch_page(driver, article)
        time.sleep(5)
        # 滚动
        driver.execute_script('document.documentElement.scrollTop=10000')
        time.sleep(60)
        driver.close()
        print('read done')
        switch_page(driver)
    driver.close()
    switch_page(driver)


def watch_videos(driver):
    driver.find_element_by_class_name('list').click()
    videos = driver.find_elements_by_class_name('text-link-item-title')
    for i in range(6):
        print(f'start watch video {videos[i].text}...')
        switch_page(driver, videos[i])
        time.sleep(5)
        driver.execute_script('document.documentElement.scrollTop=10000')
        time.sleep(60)
        driver.close()
        print('read done')
        switch_page(driver)
    driver.back()
    switch_page(driver)


if __name__ == '__main__':
    # 换成自己的路径
    driver = webdriver.Chrome('chromedriver.exe')
    # driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    base_url = 'https://www.xuexi.cn/'
    driver.get(base_url)
    time.sleep(5)
    page_num = login_part(driver)
    # point_btn = driver.find_element_by_xpath(
    #     '//*[@id="root"]/div/div/section/div/div/div/div/div[4]/section/div[4]')
    # switch_page(driver, point_btn)
    # score_items = driver.find_elements_by_class_name('my-points-card')

    # 读文章
    # switch_page(driver, score_items[1].find_element_by_class_name('buttonbox'))
    # read_articles(driver)
    switch_page(driver, driver.find_element_by_xpath(
        '//*[@id="231c"]/div/div/div/div/div/section/div/div/div/div/div[1]/div/div/div[2]'))
    read_articles(driver)

    # 看视频
    driver.implicitly_wait(10)
    driver.get(
        'https://www.xuexi.cn/4426aa87b0b64ac671c96379a3a8bd26/db086044562a57b441c24f2af1c8e101.html')
    time.sleep(4)
    watch_videos(driver)

    print('done')
